package cz.vutbr.fit.xzemko01.tfmcbs.diagram.providers;

import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ArtifactEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponent2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.CompositeComponentEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.DelegateEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.DependencyEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.GeneralizationEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.Interface2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.InterfaceEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ManifestationEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ModelEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.ProvidingPortEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.RealizationEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.RequiringPortEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.SimpleComponent2EditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.SimpleComponentEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.edit.parts.UsageEditPart;
import cz.vutbr.fit.xzemko01.tfmcbs.diagram.part.ComponentBasedSystemDiagramEditorPlugin;
import cz.vutbr.fit.xzemko01.tfmcbs.model.meta.TfmcbsPackage;

/**
 * @generated
 */
public class ComponentBasedSystemElementTypes {

	/**
	 * @generated
	 */
	private ComponentBasedSystemElementTypes() {
	}

	/**
	 * @generated
	 */
	private static Map<IElementType, ENamedElement> elements;

	/**
	 * @generated
	 */
	private static ImageRegistry imageRegistry;

	/**
	 * @generated
	 */
	private static Set<IElementType> KNOWN_ELEMENT_TYPES;

	/**
	 * @generated
	 */
	public static final IElementType Model_1000 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Model_1000"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType SimpleComponent_2001 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.SimpleComponent_2001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType CompositeComponent_2002 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.CompositeComponent_2002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Artifact_2003 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Artifact_2003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Interface_2004 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Interface_2004"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType SimpleComponent_3001 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.SimpleComponent_3001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType CompositeComponent_3002 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.CompositeComponent_3002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Interface_3003 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Interface_3003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType ProvidingPort_3004 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.ProvidingPort_3004"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType RequiringPort_3005 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.RequiringPort_3005"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Dependency_4001 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Dependency_4001"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Manifestation_4002 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Manifestation_4002"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Realization_4003 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Realization_4003"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Usage_4004 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Usage_4004"); //$NON-NLS-1$
	/**
	 * @generated
	 */
	public static final IElementType Generalization_4005 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Generalization_4005"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static final IElementType Delegate_4006 = getElementType("cz.vutbr.fit.xzemko01.tool-for-modelling-of-component-based-systems.diagram.Delegate_4006"); //$NON-NLS-1$

	/**
	 * @generated
	 */
	private static ImageRegistry getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new ImageRegistry();
		}
		return imageRegistry;
	}

	/**
	 * @generated
	 */
	private static String getImageRegistryKey(ENamedElement element) {
		return element.getName();
	}

	/**
	 * @generated
	 */
	private static ImageDescriptor getProvidedImageDescriptor(
			ENamedElement element) {
		if (element instanceof EStructuralFeature) {
			EStructuralFeature feature = ((EStructuralFeature) element);
			EClass eContainingClass = feature.getEContainingClass();
			EClassifier eType = feature.getEType();
			if (eContainingClass != null && !eContainingClass.isAbstract()) {
				element = eContainingClass;
			} else if (eType instanceof EClass
					&& !((EClass) eType).isAbstract()) {
				element = eType;
			}
		}
		if (element instanceof EClass) {
			EClass eClass = (EClass) element;
			if (!eClass.isAbstract()) {
				return ComponentBasedSystemDiagramEditorPlugin.getInstance()
						.getItemImageDescriptor(
								eClass.getEPackage().getEFactoryInstance()
										.create(eClass));
			}
		}
		// TODO : support structural features
		return null;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(ENamedElement element) {
		String key = getImageRegistryKey(element);
		ImageDescriptor imageDescriptor = getImageRegistry().getDescriptor(key);
		if (imageDescriptor == null) {
			imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
		}
		return imageDescriptor;
	}

	/**
	 * @generated
	 */
	public static Image getImage(ENamedElement element) {
		String key = getImageRegistryKey(element);
		Image image = getImageRegistry().get(key);
		if (image == null) {
			ImageDescriptor imageDescriptor = getProvidedImageDescriptor(element);
			if (imageDescriptor == null) {
				imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
			}
			getImageRegistry().put(key, imageDescriptor);
			image = getImageRegistry().get(key);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public static ImageDescriptor getImageDescriptor(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImageDescriptor(element);
	}

	/**
	 * @generated
	 */
	public static Image getImage(IAdaptable hint) {
		ENamedElement element = getElement(hint);
		if (element == null) {
			return null;
		}
		return getImage(element);
	}

	/**
	 * Returns 'type' of the ecore object associated with the hint.
	 * 
	 * @generated
	 */
	public static ENamedElement getElement(IAdaptable hint) {
		Object type = hint.getAdapter(IElementType.class);
		if (elements == null) {
			elements = new IdentityHashMap<IElementType, ENamedElement>();

			elements.put(Model_1000, TfmcbsPackage.eINSTANCE.getModel());

			elements.put(SimpleComponent_2001,
					TfmcbsPackage.eINSTANCE.getSimpleComponent());

			elements.put(CompositeComponent_2002,
					TfmcbsPackage.eINSTANCE.getCompositeComponent());

			elements.put(Artifact_2003, TfmcbsPackage.eINSTANCE.getArtifact());

			elements.put(Interface_2004, TfmcbsPackage.eINSTANCE.getInterface());

			elements.put(SimpleComponent_3001,
					TfmcbsPackage.eINSTANCE.getSimpleComponent());

			elements.put(CompositeComponent_3002,
					TfmcbsPackage.eINSTANCE.getCompositeComponent());

			elements.put(Interface_3003, TfmcbsPackage.eINSTANCE.getInterface());

			elements.put(ProvidingPort_3004,
					TfmcbsPackage.eINSTANCE.getProvidingPort());

			elements.put(RequiringPort_3005,
					TfmcbsPackage.eINSTANCE.getRequiringPort());

			elements.put(Dependency_4001,
					TfmcbsPackage.eINSTANCE.getDependency());

			elements.put(Manifestation_4002,
					TfmcbsPackage.eINSTANCE.getManifestation());

			elements.put(Realization_4003,
					TfmcbsPackage.eINSTANCE.getRealization());

			elements.put(Usage_4004, TfmcbsPackage.eINSTANCE.getUsage());

			elements.put(Generalization_4005,
					TfmcbsPackage.eINSTANCE.getGeneralization());

			elements.put(Delegate_4006, TfmcbsPackage.eINSTANCE.getDelegate());
		}
		return (ENamedElement) elements.get(type);
	}

	/**
	 * @generated
	 */
	private static IElementType getElementType(String id) {
		return ElementTypeRegistry.getInstance().getType(id);
	}

	/**
	 * @generated
	 */
	public static boolean isKnownElementType(IElementType elementType) {
		if (KNOWN_ELEMENT_TYPES == null) {
			KNOWN_ELEMENT_TYPES = new HashSet<IElementType>();
			KNOWN_ELEMENT_TYPES.add(Model_1000);
			KNOWN_ELEMENT_TYPES.add(SimpleComponent_2001);
			KNOWN_ELEMENT_TYPES.add(CompositeComponent_2002);
			KNOWN_ELEMENT_TYPES.add(Artifact_2003);
			KNOWN_ELEMENT_TYPES.add(Interface_2004);
			KNOWN_ELEMENT_TYPES.add(SimpleComponent_3001);
			KNOWN_ELEMENT_TYPES.add(CompositeComponent_3002);
			KNOWN_ELEMENT_TYPES.add(Interface_3003);
			KNOWN_ELEMENT_TYPES.add(ProvidingPort_3004);
			KNOWN_ELEMENT_TYPES.add(RequiringPort_3005);
			KNOWN_ELEMENT_TYPES.add(Dependency_4001);
			KNOWN_ELEMENT_TYPES.add(Manifestation_4002);
			KNOWN_ELEMENT_TYPES.add(Realization_4003);
			KNOWN_ELEMENT_TYPES.add(Usage_4004);
			KNOWN_ELEMENT_TYPES.add(Generalization_4005);
			KNOWN_ELEMENT_TYPES.add(Delegate_4006);
		}
		return KNOWN_ELEMENT_TYPES.contains(elementType);
	}

	/**
	 * @generated
	 */
	public static IElementType getElementType(int visualID) {
		switch (visualID) {
		case ModelEditPart.VISUAL_ID:
			return Model_1000;
		case SimpleComponentEditPart.VISUAL_ID:
			return SimpleComponent_2001;
		case CompositeComponentEditPart.VISUAL_ID:
			return CompositeComponent_2002;
		case ArtifactEditPart.VISUAL_ID:
			return Artifact_2003;
		case InterfaceEditPart.VISUAL_ID:
			return Interface_2004;
		case SimpleComponent2EditPart.VISUAL_ID:
			return SimpleComponent_3001;
		case CompositeComponent2EditPart.VISUAL_ID:
			return CompositeComponent_3002;
		case Interface2EditPart.VISUAL_ID:
			return Interface_3003;
		case ProvidingPortEditPart.VISUAL_ID:
			return ProvidingPort_3004;
		case RequiringPortEditPart.VISUAL_ID:
			return RequiringPort_3005;
		case DependencyEditPart.VISUAL_ID:
			return Dependency_4001;
		case ManifestationEditPart.VISUAL_ID:
			return Manifestation_4002;
		case RealizationEditPart.VISUAL_ID:
			return Realization_4003;
		case UsageEditPart.VISUAL_ID:
			return Usage_4004;
		case GeneralizationEditPart.VISUAL_ID:
			return Generalization_4005;
		case DelegateEditPart.VISUAL_ID:
			return Delegate_4006;
		}
		return null;
	}

}
